from datetime import datetime, timedelta
import os, sys, shutil, uuid, sqlite3, time
from procdb import ProcessObject


class Task:
	def __init__(self):
		self.oid		= None
		self.status		= 0
		self.task_type	= 1
		self.name		= ""
		self.method		= ""
		self.arg		= ""
		self.start_after= None
		self.guid		= None
		self.last_run	= ''
		self.start_run	= ''
		self.priority	= 500
		self.start_time	= ''
		self.recurrance	= 0
		self.notify		= ''

	def GetModel(self):
		return ProcessObject("task")
				
	def GetTasks(self):
		return self.GetModel().GetObjectList("id>0")

	def GetPendingTasks(self):
		timeVal		= time.strftime("%Y-%m-%d %H:%M:00", time.localtime())
		criteria	= "start_after > '" +timeVal+"'"
		return self.GetModel().GetObjectList(criteria)

	def GetAllTasks(self):
		return self.GetModel().GetObjectList("id>0")
						
	def Create(self):
		if self.start_time == None:
			self.start_time = datetime.now()

		self.guid = str(uuid.uuid1()).replace( '-', '' ).lower();
				
		fields	= {
				'guid': self.guid,
				'status': self.status,
				'task_type': self.task_type,
				'name': self.name,
				'method': self.method,
				'arg': self.arg,
				'start_after': self.start_time,
				'creation_time': datetime.now(),
				'last_run': '',
				'priority': self.priority,
				'start_time': self.start_time,
				'recurrance': self.recurrance,
				'notify':self.notify
				}
		return self.GetModel().Add( fields )

		
	def Update(self):
		fields	= {
				'guid': self.guid,
				'status': self.status,
				'task_type': self.task_type,
				'name': self.name,
				'method': self.method,
				'arg': self.arg,
				'start_after': self.start_time,
				'creation_time': datetime.now(),
				'last_run': '',
				'priority': self.priority,
				'start_time': self.start_time,
				'recurrance': self.recurrance,
				'notify':self.notify
				}
		return self.GetModel().Update( self.oid, fields )


	def Load(self,oid):
		model	= self.GetModel()
		
		c	= model.conn.cursor()
		t   = (oid,)
		c.execute("SELECT guid,status,task_type,name,method,arg,start_after,creation_time,last_run,priority,start_time,recurrance,notify FROM " + model.table +" WHERE id=?", t  )
		r	= c.fetchone()
		if r == None:
			return

		self.oid		= oid	
		self.guid		= r[0]
		self.status		= r[1]
		self.task_type	= r[2]
		self.name		= r[3]
		self.method		= r[4]
		self.arg		= r[5]
		self.start_after= r[6]
		self.last_run	= r[7]
		self.start_run	= r[8]
		self.priority	= r[9]
		self.start_time	= r[10]
		self.recurrance	= r[11]
		self.notify		= r[12]
		return 
								
	def Delete(self):
		self.GetModel().Delete( self.oid )

class ScheduledTime:
	@staticmethod
	def Now():
		return time.strftime("%Y-%m-%d %H:%M:00", time.localtime())

	@staticmethod
	def NextHour():
		TimeAt = datetime.now() + timedelta(hours=1)
		return TimeAt.strftime("%Y-%m-%d %H:%M:00")

	@staticmethod
	def Tomorrow():
		TimeAt = datetime.now() + timedelta(days=1)
		return TimeAt.strftime("%Y-%m-%d %H:%M:00")

	@staticmethod
	def NextWeek():
		TimeAt = datetime.now() + timedelta(days=7)
		return TimeAt.strftime("%Y-%m-%d %H:%M:00")

	@staticmethod
	def NextMonth():
		TimeNow = datetime.now()
		if (TimeNow.month==12):
			Month   = 1
			Year  	= TimeNow.year+1
		else:
			Month   = TimeNow.month+1
			Year  	= TimeNow.year
	
		TimeAt = datetime(Year,Month,TimeNow.day,TimeNow.hour,TimeNow.minute,TimeNow.second)
		return TimeAt.strftime("%Y-%m-%d %H:%M:00")
										
